<?php
namespace app\macht\controller;
use app\common\controller\Platurl;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\CardGeneral;
use app\common\model\CardConsume;
use app\common\model\Agent;
use app\common\model\Merchant;
use app\common\model\OpenTemplate;
use app\common\model\WxOpen;
use app\common\model\System;
use app\common\controller\Carddate;
use app\common\controller\AdminBase;

use think\Controller;
use think\Session;
use Wechat\Wxobjopen;
class Wxtemlpmesg extends MachBase
{


    //单独发送卡券
    public function putCard($arr){
        //wxopenid
        $Merchant = new Merchant();
        $mid = $this->getmid($arr['card_id']);
        $wx_open_id = $Merchant->where('id',$mid)->value('wx_open_id');
        //初始化微信类
        $options = $this->options($arr['card_id']);
        $weObj = new Wxobjopen($options);
        //模板ID
        $OpenTemplate= new OpenTemplate();
        $templateid=$OpenTemplate->where(['type'=>'card_zs_tpl','wx_open_id'=>$wx_open_id])->value('template_id');
        $Platurl = new Platurl();
        $agent_url = $Platurl->getagenturl('card_id',$arr['card_id']);
        $template = array(
            'touser' => $arr['openid'],
            'template_id' => $templateid,
            'url' => $agent_url."/mobile.php/carduser/addcard/card_id/".$arr['card_id']."/openid/".$arr['openid'],
            'topcolor' => "#7B68EE",
            'data' => array(
                'first' => ['value' => "卡券赠送提醒", 'color' => "#743A3A"],
                'keyword1' => ['value' => $arr['name'], 'color' => '#000'],
                'keyword2' => ['value' => $arr['title'], 'color' => '#FF0000'],
                'keyword3' => ['value' => date("Y-m-d H:i:s", time()), 'color' => '#000'],
                'remark' => ['value' => "祝您生活愉快", 'color' => '#000'],
            )
        );
        //发送
        $res = $weObj->sendTemplateMessage($template);
        return $res;
    }

    //充值赠送   发送多张卡券

    //发送卡券列表
    public function putCardlist($arr){
        $Merchant = new Merchant();
        $mid = $this->getmid($arr['card_id']);
        $wx_open_id = $Merchant->where('id',$mid)->value('wx_open_id');
        //初始化微信类
        $options = $this->options($arr['card_id']);
        $weObj = new Wxobjopen($options);
        //模板ID
        $OpenTemplate= new OpenTemplate();
        $templateid=$OpenTemplate->where(['type'=>'card_zs_tpl','wx_open_id'=>$wx_open_id])->value('template_id');
        $Platurl = new Platurl();
        $agent_url = $Platurl->getagenturl('card_id',$arr['card_id']);
        $template = array(
            'touser' => $arr['openid'],
            'template_id' => $templateid,
            'url' => $agent_url."/mobile.php/carduser/addcardlist/give/".$arr['give']."/openid/".$arr['openid'].'/card_id/'.$arr['card_id'],
            'topcolor' => "#7B68EE",
            'data' => array(
                'first' => ['value' => "卡券赠送提醒", 'color' => "#743A3A"],
                'keyword1' => ['value' => $arr['name'], 'color' => '#000'],
                'keyword2' => ['value' => $arr['title'], 'color' => '#FF0000'],
                'keyword3' => ['value' => date("Y-m-d H:i:s", time()), 'color' => '#000'],
                'remark' => ['value' => "祝您生活愉快", 'color' => '#000'],
            )
        );
        //发送
        $res = $weObj->sendTemplateMessage($template);
        return $res;
    }




    //商户ID
    public function getmid($card_id){
        $CardMember = new CardMember();
        $CardGeneral = new CardGeneral();
        $top_mid = $CardMember->where(['card_id'=>$card_id])->value('mid');
        if (empty($top_mid)){
            $top_mid = $CardGeneral->where(['card_id'=>$card_id])->value('mid');
            return $top_mid;
        }else{
            return $top_mid;
        }
    }

    //微信API参数  根据card_id获取
    public function options($card_id){
        $CardMember = new CardMember();
        $CardGeneral = new CardGeneral();
        $Merchant = new Merchant();
        $System=new System();
        $Agent = new Agent();
        $WxOpen=new WxOpen();
        $mid = $CardMember->where('card_id',$card_id)->value('mid');
        if ($mid){
            $map['id'] = $mid;
        }else{
            $map['id'] = $CardGeneral->where('card_id',$card_id)->value('mid');
        }
        $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] = $merchantinfo['wx_open_id'];
        $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
        $users = $WxOpen->where($mab)->find();

        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}
